import 'package:flutter/foundation.dart' show ChangeNotifier;
import 'package:flutter/foundation.dart';
import 'package:message_tools/model/sms_send_record.dart';
import 'package:message_tools/service/db_repository.dart';

class SmsSendRecordModel with ChangeNotifier {
  int _notSendCount = 0;
  int _sentCount = 0;
  int _sendFailCount = 0;
  int _sendSuccessCount = 0;

  List<SmsSendRecord> _list = [];
  List<SmsSendRecord> get list => _list;

  int get notSendCount => _notSendCount;
  int get sentCount => _sentCount;
  int get sendFailCount => _sendFailCount;
  int get sendSuccessCount => _sendSuccessCount;

  loadNotSendSms(int offset, int limit) {
    DbRepository.fetchSigleSmsSendRecordList(0, offset, limit).then((data) {
      _list = data;
      notifyListeners();
    });
  }

  loadSentSms(int offset, int limit) {
    DbRepository.fetchSmsSentRecordList(offset, limit).then((data) {
      _list = data;
      notifyListeners();
    });
  }

  loadSendFailSms(int offset, int limit) {
    DbRepository.fetchSigleSmsSendRecordList(2, offset, limit).then((data) {
      _list = data;
      notifyListeners();
    });
  }

  loadSendSuccessSms(int offset, int limit) {
    DbRepository.fetchSigleSmsSendRecordList(3, offset, limit).then((data) {
      _list = data;
      notifyListeners();
    });
  }

  void updateSmsRecordStatus(String phoneNum, int status) async {
    await DbRepository.updateSmsRecordStatus(phoneNum, status);
    await loadSendCount();
  }

  Future<List> getDataList(int status, int offset, int limit) {
    return DbRepository.fetchSmsSendRecordList(status, offset, limit);
  }

  void loadSendCount() async {
    _notSendCount = await DbRepository.getSendCount(0);
    _sentCount = await DbRepository.getSendCount(1);
    _sendSuccessCount = await DbRepository.getSendCount(2);
    _sendFailCount = await DbRepository.getSendCount(3);
    _sentCount = _sentCount + _sendSuccessCount;
    notifyListeners();
  }

  Future<int> getSendCount(int status) async {
    return DbRepository.getSendCount(status);
  }
}
